package com.example.ymm.modules1.service.impl.hl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.exception.YmException;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.hl.dkn.DknTrialItem;
import com.example.ymm.modules1.entity.hl.yw.YwMoneyAffirmLog;
import com.example.ymm.modules1.mapper.hl.DknTrialItemMapper;
import com.example.ymm.modules1.query.hl.DknTrialItemQueryCriteria;
import com.example.ymm.modules1.service.dto.heng.YwExcuteCreateDto;
import com.example.ymm.modules1.service.dto.heng.YwPostageCreateDto;
import com.example.ymm.modules1.service.dto.ms.MsMoneyAffirmDto;
import com.example.ymm.modules1.service.hl.DknTrialItemService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
@Slf4j
@DS("slave2")
public class DknTrialItemServiceImpl extends BaseServiceImpl<DknTrialItemMapper, DknTrialItem> implements DknTrialItemService {

    @Override
    public TableList<DknTrialItem> list(DknTrialItemQueryCriteria criteria, YmPageDto pageDto) {
        IPage<DknTrialItem> page = new Page<DknTrialItem>(pageDto.getPage(),pageDto.getPageSize());
        QueryWrapper predicate = QueryHelpPlus.getPredicate(DknTrialItem.class, criteria);
        predicate.orderByDesc("sort");
        baseMapper.selectPage(page, predicate);
        TableList<DknTrialItem> tableList=new TableList(page);
        return tableList;
    }

    public List<DknTrialItem> queryAll(DknTrialItemQueryCriteria criteria) {
        QueryWrapper predicate = QueryHelpPlus.getPredicate(DknTrialItem.class, criteria);
        predicate.orderByDesc("sort");
        List<DknTrialItem> result =baseMapper.selectList(predicate);
        return result;
    }

    @Override
    public void addExcludeData(YwExcuteCreateDto dto) {
        if(UtilValidate.isEmpty(dto.getTableList())||UtilValidate.isEmpty(dto.getApprovalName())){
            throw new YmException("参数不能为空!");
        }
        String userId = getUserId();
        String userName = getUserName();
        List<JSONObject> tableList = dto.getTableList();
        List<DknTrialItem> addList = new ArrayList<>();
        for (int i = 0; i < tableList.size(); i++) {
            JSONObject json = tableList.get(i);
            DknTrialItem item = new DknTrialItem();
            item.setState("排除");
            item.setOrderNo(json.getString("orderNo"));
            item.setApproveNo("排除");
            item.setUserId(userId);
            item.setUserName(userName);
            item.setSort(i);
            item.setJsonStr(json.toString());
            item.setApprovalName(dto.getApprovalName());
            addList.add(item);
        }
        saveOrUpdateBatch(addList);
    }


    @Override
    public List<JSONObject> getExcludeDataList(String approvalName) {
        DknTrialItemQueryCriteria criteria=new DknTrialItemQueryCriteria();
        criteria.setApprovalName(approvalName);
        String userId = getUserId();
        criteria.setUserId(userId);
        criteria.setState("排除");
        List<DknTrialItem> dknTrialItems = queryAll(criteria);
        List<JSONObject> list = new ArrayList<>();
        for(DknTrialItem item:dknTrialItems){
            JSONObject json=JSONObject.parseObject(item.getJsonStr());
            list.add(json);
        }
        return list;
    }


    @Override
    public List<JSONObject> getItemDataList(String approveNo) {
        DknTrialItemQueryCriteria criteria=new DknTrialItemQueryCriteria();
        criteria.setApproveNo(approveNo);
        String userId = getUserId();
        criteria.setUserId(userId);
        criteria.setState("提交");
        List<DknTrialItem> dknTrialItems = queryAll(criteria);
        List<JSONObject> list = new ArrayList<>();
        for(DknTrialItem item:dknTrialItems){
            JSONObject json=JSONObject.parseObject(item.getJsonStr());
            list.add(json);
        }
        return list;
    }

    @Override
    public void removeExcludeData(String orderNo) {
        LambdaQueryWrapper<DknTrialItem> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(DknTrialItem::getOrderNo, orderNo);
        remove(queryWrapper);
    }
}
